package com.etc.admin.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.etc.admin.service.BookRoomShowService;
import com.etc.admin.service.OrderService;
import com.etc.admin.service.RoomService;
import com.etc.admin.service.UserService;
import com.etc.entity.BookRoomShow;
import com.etc.entity.Order;
import com.etc.entity.OrderZhifu;
import com.etc.entity.Room;
import com.etc.entity.User;
import com.etc.util.CommonMessage;

@Controller
public class BookRoomShowController {
	@Resource(name="BookRoomShowService")
	private BookRoomShowService brs;
	@Resource(name="OrderService")
	private OrderService os;
	@Resource(name="roomService")
	private RoomService rs;
	@Resource(name="userService")
	private UserService userService;
	/**
	 * 传递给房间展示的数据的控制器
	 * @return
	 */
	@RequestMapping(value="showMyRoom")
	public ModelAndView  showRoom(){
		ModelAndView mv=new ModelAndView("views/myshow");
	List<BookRoomShow> list=brs.getCurentOrders();
	mv.addObject("list", list);
	return mv;
	}
	/**
	 * 退房
	 * @return
	 */
	@RequestMapping(value="checkOut")
	@ResponseBody
	public CommonMessage  checkOut(int orderid,int roomno){
		
		CommonMessage cm = new CommonMessage();
		boolean flag1=os.updateOrderById(orderid,0);//修改订单的状态为0，即退房状态
		boolean flag2=rs.upStatusByRoomno(roomno,0);//修改房间的状态为0，即空房状态
		if(flag1==true && flag2==true) {
			
			cm.setMsg("退房成功");
		}else {
			cm.setMsg("退房失败");
		}
	return cm;
	}
	
	/**
	 * 预定单，办理入住
	 * @return
	 */
	@RequestMapping(value="checkIn")
	@ResponseBody
	public CommonMessage  checkIn(int orderid,int roomno){
		
		CommonMessage cm = new CommonMessage();
		boolean flag1=os.updateOrderById(orderid,1);//修改订单的状态为1，即入住状态
		boolean flag2=rs.upStatusByRoomno(roomno,1);//修改房间的状态为1，即入住状态
		if(flag1==true && flag2==true) {
			cm.setMsg("入住成功");
		}else {
			cm.setMsg("入住失败");
		}
	return cm;
	}
	
	/**
	 * book.jsp传过来的数据，根据入住的时间和退房的时间，选择合适的房间
	 * @return
	 */
	@RequestMapping(value="adminbooknow")
	@ResponseBody
	public ModelAndView  booknow(String orderstartdate,String orderenedate){
		ModelAndView mv=new ModelAndView("views/selectroom");
		List<Room> list1=brs.getRoomByTime(orderstartdate, orderenedate, 1);//得到单人间
		List<Room> list2=brs.getRoomByTime(orderstartdate, orderenedate, 2);//得到双人间
		List<Room> list3=brs.getRoomByTime(orderstartdate, orderenedate, 3);//得到三人间
		List<Room> list0=brs.getRoomByTime(orderstartdate, orderenedate, 0);//得到总统套房
		mv.addObject("list1", list1);
		mv.addObject("list2", list2);
		mv.addObject("list3", list3);
		mv.addObject("list0", list0);
		mv.addObject("orderstartdate", orderstartdate);
		mv.addObject("orderenedate", orderenedate);
	return mv;
	}
	
	
	/**
	 * book.jsp传过来的数据，根据入住的时间和退房的时间，选择合适的房间
	 * @return
	 */
	@RequestMapping(value="adminbookornow")
	@ResponseBody
	public CommonMessage  adminbooknow(@RequestBody OrderZhifu order){
		CommonMessage cm = new CommonMessage();
		String orderenedate=order.getOrderenedate();
		int roomno=order.getRoomno();
		String username=order.getUsername();
		int empid=order.getEmpid();
		String usersex=order.getUsersex();
		String usertel=order.getUsertel();
		String orderstartdate=order.getOrderstartdate();
		
		//	先取得用户的id,如果没有，就给用户创建一个id;
		User user=new User(0, username, "123", usersex, usertel, 0);
		int userid=userService.getUserIdByname(user);//得到用户的id
		int roomid=rs.selectRoomByNo(roomno).getRoomid();//得到房间的id
		SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
		String orderdate=s.format(new Date());//得到当天的日期，预定单位当天的日期
		Order order1=new Order(0, empid, userid, roomid, 2, orderdate, orderstartdate, orderenedate);
		boolean flag=os.insertSelective(order1);//创建订单，订单的状态固定为2；为预定的状态
		if(flag) {
			cm.setMsg("预定成功");
			}else {
			cm.setMsg("预定失败");
			}
		return cm;
		
	}
	
	
		
	
}
